package com.zenithmind.coding.controller;

import com.zenithmind.common.result.Result;
import com.zenithmind.coding.service.CodeFormatterService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 代码格式化控制器 - 遵循单一职责原则，专门处理代码格式化相关功能
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Tag(name = "代码格式化", description = "代码格式化和美化相关接口")
@RestController
@RequestMapping("/api/zenithMind/v1/code/formatter")
@RequiredArgsConstructor
public class CodeFormatterController {

    private final CodeFormatterService codeFormatterService;

    @Operation(summary = "格式化代码", description = "格式化指定语言的代码")
    @PostMapping("/format")
    public Result<String> formatCode(
            @Parameter(description = "编程语言") @RequestParam String language,
            @Parameter(description = "源代码") @RequestBody String sourceCode) {
        String formattedCode = codeFormatterService.formatCode(language, sourceCode);
        return Result.success(formattedCode);
    }

    @Operation(summary = "美化代码", description = "美化代码，包括格式化和优化")
    @PostMapping("/beautify")
    public Result<String> beautifyCode(
            @Parameter(description = "编程语言") @RequestParam String language,
            @Parameter(description = "源代码") @RequestBody String sourceCode,
            @Parameter(description = "格式化选项") @RequestParam(required = false) String options) {
        String beautifiedCode = codeFormatterService.beautifyCode(language, sourceCode, options);
        return Result.success(beautifiedCode);
    }

    @Operation(summary = "压缩代码", description = "压缩代码，移除不必要的空白和注释")
    @PostMapping("/minify")
    public Result<String> minifyCode(
            @Parameter(description = "编程语言") @RequestParam String language,
            @Parameter(description = "源代码") @RequestBody String sourceCode) {
        String minifiedCode = codeFormatterService.minifyCode(language, sourceCode);
        return Result.success(minifiedCode);
    }

    @Operation(summary = "验证代码格式", description = "验证代码格式是否符合规范")
    @PostMapping("/validate")
    public Result<Boolean> validateFormat(
            @Parameter(description = "编程语言") @RequestParam String language,
            @Parameter(description = "源代码") @RequestBody String sourceCode) {
        Boolean isValid = codeFormatterService.validateFormat(language, sourceCode);
        return Result.success(isValid);
    }

    @Operation(summary = "获取格式化选项", description = "获取指定语言的格式化选项")
    @GetMapping("/options/{language}")
    public Result<Object> getFormattingOptions(
            @Parameter(description = "编程语言") @PathVariable String language) {
        Object options = codeFormatterService.getFormattingOptions(language);
        return Result.success(options);
    }

    @Operation(summary = "设置格式化选项", description = "设置指定语言的格式化选项")
    @PostMapping("/options/{language}")
    public Result<Boolean> setFormattingOptions(
            @Parameter(description = "编程语言") @PathVariable String language,
            @Parameter(description = "格式化选项") @RequestBody Object options) {
        Boolean success = codeFormatterService.setFormattingOptions(language, options);
        return Result.success(success);
    }

    @Operation(summary = "获取支持的格式化器", description = "获取支持的代码格式化器列表")
    @GetMapping("/formatters")
    public Result<String[]> getSupportedFormatters() {
        String[] formatters = codeFormatterService.getSupportedFormatters();
        return Result.success(formatters);
    }

    @Operation(summary = "检查格式化器支持", description = "检查指定语言是否支持格式化")
    @GetMapping("/formatters/{language}/supported")
    public Result<Boolean> isFormatterSupported(
            @Parameter(description = "编程语言") @PathVariable String language) {
        Boolean supported = codeFormatterService.isFormatterSupported(language);
        return Result.success(supported);
    }
}
